package com.gy.bearhelps.news;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;

import com.google.gson.reflect.TypeToken;
import com.gy.bearhelps.R;
import com.gy.bearhelps.base.BaseBean;
import com.gy.bearhelps.base.BaseFragment;
import com.gy.bearhelps.base.ItemBean;
import com.gy.bearhelps.util.FragmentAdapter;
import com.gy.bearhelps.util.RequestResultListener;
import com.gy.bearhelps.util.Urls;
import com.gy.bearhelps.view.YouPinTitleView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.http.RequestParams;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 高岳 on 2016/11/2.
 * Describe:新闻主页
 */
public class NewsHomeFragment extends BaseFragment implements YouPinTitleView.YouPinTitleSelectListener{

    private YouPinTitleView youPinTitleView;
    private ViewPager viewPager;
    private FragmentAdapter fragmentAdapter;
    private List<Fragment> fragments;
    private List<String> titleList;
    private List<ItemBean> titles;

    @Override
    protected View setView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.fg_news_home,null);
        setTitleMsg("新闻动态");
        return view;
    }

    @Override
    protected void initView() {
        youPinTitleView = (YouPinTitleView) _rootView.findViewById(R.id.fg_news_home_titleview);
        viewPager = (ViewPager) _rootView.findViewById(R.id.fg_news_home_viewpage);
    }

    @Override
    protected void initData() {
        titleList = new ArrayList<>();
        fragments = new ArrayList<>();
//        titleList.add("新闻动态");
//        titleList.add("通知公告");
//        titleList.add("工作动态");
//        fragments.add(new NewsItemFragment());
//        fragments.add(new NewsItemFragment());
//        fragments.add(new NewsItemFragment());
        fragmentAdapter = new FragmentAdapter(this.getActivity().getSupportFragmentManager(),fragments);
    }

    @Override
    protected void setListener() {
        super.setListener();
        youPinTitleView.setMyListener(this);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }
            @Override
            public void onPageSelected(int position) {
                youPinTitleView.setSelect(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void fillData() {
        super.fillData();
        _baseBack.setVisibility(View.INVISIBLE);
        youPinTitleView.setmDatas(titleList);
        viewPager.setAdapter(fragmentAdapter);
        requestTitles();
    }

    @Override
    public void titleSelect(int item) {
        viewPager.setCurrentItem(item);
    }

    /** 请求标题*/
    private void requestTitles(){
        showLoadDialog();
        RequestParams params = new RequestParams(Urls.select_titles);
        params.addQueryStringParameter("type",2+"");
        getRequest(params, new RequestResultListener() {
            @Override
            public boolean onSuccess(BaseBean result) {
                Type type = new TypeToken<List<ItemBean>>(){}.getType();
                try {
                    JSONArray array = new JSONArray(result.data);
                    JSONObject obj = array.getJSONObject(0);
                    titles = gson.fromJson(obj.optString("list"),type);
                    titleList.clear();
                    fragments.clear();
                    for(ItemBean bean : titles){
                        titleList.add(bean.channelName);
                        fragments.add(new NewsItemFragment(bean.channelId,bean.channelName));
                    }
                    youPinTitleView.setmDatas(titleList);
                    fragmentAdapter = new FragmentAdapter(getActivity().getSupportFragmentManager(),fragments);
                    viewPager.setAdapter(fragmentAdapter);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                return false;
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }
        });
    }
}

